var AV = require('leanengine');
var rs = require('../../cloud/qiniu/rs');
var superagent = require( 'superagent' );
var qiniuIo = require('../../cloud/qiniu/io');
var common   = require( '../../cloud/common' );
var apis = require( '../../cloud/lib/apis' );
exports.getUploadToken = getUploadToken;
function  getUploadToken(key){
	var newBucketName={};
	newBucketName["scope"]=apis['qiniu']['upSpace'];
	if(key!=null)
		newBucketName["scope"]+=":"+key;
	var putPolicy = new rs.PutPolicy(newBucketName,1,null,null,null,null);
	var uptoken=putPolicy.token();
	return uptoken;
}
exports.getUploadToken1 = getUploadToken1;
function  getUploadToken1(key){
	var newBucketName={};
	newBucketName["scope"]=apis['qiniu']['upSpace'];
	if(key!=null)
		newBucketName["scope"]+=":"+key;
	var putPolicy = new rs.PutPolicy(newBucketName,-11,null,null,null,null);
	var uptoken=putPolicy.token();
	return uptoken;
}
// @gist uptoken
function uptoken(bucketname,type,uploadType,platform,version,audioOrMedia,callback) {
  var newBucketName={};
  newBucketName["scope"]=bucketname;
  var putPolicy = new rs.PutPolicy(newBucketName,type,uploadType,platform,version,audioOrMedia);
  var uptoken=putPolicy.token();
  var returnArr=common.getReturnArr("","1000","",uptoken);
  return callback(null,JSON.stringify(returnArr));
}
//@gist uptoken
function uptokens(bucketname,type,uploadType,platform,version,audioOrMedia,callback) {
	if(typeof bucketname !="object")
		bucketname=JSON.parse(bucketname);
	var returnArry=[];
	for ( var i = 0; i < bucketname.length; i++) {
		  var returnToken={};
		  var newBucketName={};
		  newBucketName["scope"]=apis['qiniu']['upSpace']+":"+bucketname[i];
		  var putPolicy = new rs.PutPolicy(newBucketName,type,uploadType,platform,version,audioOrMedia);
		  var uptoken=putPolicy.token();
		  returnToken[bucketname[i]]=uptoken;
		  returnArry.push(returnToken);
	}
  var returnArr=common.getReturnArr("","1000","",returnArry);
  return callback(null,JSON.stringify(returnArr));
}
//获取七牛上传token
AV.Cloud.define("getQiniuUptoken",function(request, response) {
	if(request.user!=null)
	uptoken(request.params.scope,1,request.params.type,request.params.platform,request.params.version,request.params.audioOrMedia,function(error,returnStr) {
		if(error){
			return response.error(returnStr);
		}else{
			return response.success(returnStr);
		}
	});
});
//获取七牛上传tokens
AV.Cloud.define("getQiniuUptokens",function(request, response) {
	uptokens(request.params.scope,1,request.params.type,request.params.platform,request.params.version,request.params.audioOrMedia,function(error,returnStr) {
		if(error){
			return response.error(returnStr);
		}else{
			return response.success(returnStr);
		}
	});
});
//获取七牛上传tokens
AV.Cloud.define("chatQiniuUptokens",function(request, response) {
	uptokens(request.params.scope,3,request.params.type,request.params.platform,request.params.version,request.params.audioOrMedia,function(error,returnStr) {
		if(error){
			return response.error(returnStr);
		}else{
			return response.success(returnStr);
		}
	});
});
//获取七牛上传tokens
AV.Cloud.define("uploadGoodsPicGetQiniuUptokens",function(request, response) {
	uptokens(request.params.scope,2,request.params.type,request.params.platform,request.params.version,request.params.audioOrMedia,function(error,returnStr) {
		if(error){
			return response.error(returnStr);
		}else{
			return response.success(returnStr);
		}
	});
});
//获取七牛上传tokens 朋友圈
AV.Cloud.define("getQiniuUptokens_circle",function(request, response) {
	uptokens(request.params.scope,4,request.params.type,request.params.platform,request.params.version,request.params.audioOrMedia,function(error,returnStr) {
		if(error){
			return response.error(returnStr);
		}else{
			return response.success(returnStr);
		}
	});
});
//获取七牛上传tokens 朋友圈
AV.Cloud.define("getQiniuUptokens_circleCover",function(request, response) {
	uptokens(request.params.scope,5,request.params.type,request.params.platform,request.params.version,request.params.audioOrMedia,function(error,returnStr) {
		if(error){
			return response.error(returnStr);
		}else{
			return response.success(returnStr);
		}
	});
});
/**
 * 上传二维码到七牛并返回结果
 * @param key 图片名
 * @param imgUrl 图片网络地址
 * @param callback
 */
exports.getQrcodeUrl=getQrcodeUrl; 
function getQrcodeUrl(key,imgUrl,callback){
	var uptoken=getUploadToken(key);
	var extra = new qiniuIo.PutExtra();
	//extra.params = params;
	//extra.mimeType = mimeType;
	//extra.crc32 = crc32;
	//extra.checkCrc = checkCrc;


	var agent = superagent.agent()
	.get(imgUrl);
	agent
	.end( function( err,result ) {
		if( err ) {

			console.log("错误信息："+err);

			return callback(err,null);
		}
		else {
			qiniuIo.put(uptoken, key, result.body, extra, function(err, ret) {
			    if (!err) {
			         // 上传成功， 处理返回值
			         return callback(null,ret.key);
			    } else {

					console.log("错误信息："+err);


					// 上传失败， 处理返回代码
			    	 return callback(err,null);
			    }
			});
		}
	} );
}



exports.up2Weima = function(key,baseFile,callback){
	var uptoken=getUploadToken(key);
	var extra = new qiniuIo.PutExtra();
	qiniuIo.put(uptoken, key, baseFile, extra, function(err, ret) {
		if (!err) {
			// 上传成功， 处理返回值

			return callback(null,ret.key);
		} else {

			console.log("错误信息："+err);


			// 上传失败， 处理返回代码
			return callback(err,null);
		}
	});

}



///**
// * 获取下载授权URL
//  */
//@gist downloadUrl
//function downloadUrl(key,callback) {
//  domain=apis['qiniu']['url'];
//  var baseUrl = rs.makeBaseUrl(domain, key);
//  var policy = new rs.GetPolicy();
//  var url= policy.makeRequest(baseUrl);
//  var returnArr=common.getReturnArr("","1000","",url);
//  return callback(null,JSON.stringify(returnArr));
//}
//// @endgist

